package com.biubiushop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.biubiushop.constant.RedisKeyConstant;
import com.biubiushop.entity.Commodity;
import com.biubiushop.entity.Equipment;
import com.biubiushop.exception.ServiceException;
import com.biubiushop.repository.ICommodityRepository;
import com.biubiushop.repository.IEquipmentRepository;
import com.biubiushop.service.ICommodityService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class CommodityServiceImpl implements ICommodityService {

    private static Log log = LogFactory.get();

    @Resource
    ICommodityRepository commodityRepository;

    @Resource
    IEquipmentRepository equipmentRepository;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public JSONObject getCommodityList(String equipmentId) throws Exception {
        //返回机器剩余纸巾长度可完成的订单
        Equipment equipment = equipmentRepository.getOne(equipmentId);
        if (ObjectUtil.isNull(equipment)) {
            throw new ServiceException("设备信息有误");
        }
        if (!equipmentId.equals("4028824f6d819f4f016db3b07a4c0224")) {
            //获取设备最新状态
            String state = stringRedisTemplate.opsForValue().get(RedisKeyConstant.getEquipmentStateKey(equipment.getSocketId()));
            if (!"0".equals(state)) {
                throw new ServiceException("设备暂无法使用");
            }
        }
        Integer overLength = equipment.getOverLength();
        List<Commodity> commodityList = commodityRepository.findAll();
        JSONObject json = new JSONObject();
        json.put("commodityList", commodityList);
        json.put("overLength", overLength);
        json.put("equipmentId", equipmentId);
        return json;
    }

    @Override
    public String editCommodity(String commodityId, String name, BigDecimal price, Integer length, String description) throws Exception {
        Commodity commodity;
        if (StrUtil.isNotEmpty(commodityId)) {
            commodity = commodityRepository.getOne(commodityId);
            if (ObjectUtil.isNull(commodity)) {
                throw new ServiceException("商品信息有误");
            }
        } else {
            commodity = new Commodity();
        }
        commodity.setName(name);
        commodity.setPrice(price);
        commodity.setLength(length);
        commodity.setDescription(description);
        commodity.setCreateTime(new Date());
        commodityRepository.save(commodity);
        return "编辑商品信息成功";
    }

    public static void main(String[] args) {
        CompletableFuture.supplyAsync(()->"Qwer")
                .thenApplyAsync(s->s+"ASDF")
                .thenApplyAsync(String::toLowerCase)
                .thenAcceptAsync(System.out::println);
    }
}
